package com.itaem.community.modules.complaint.controller;



import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itaem.community.core.annotations.ResponseResult;
import com.itaem.community.modules.complaint.entity.ComplaintEntity;
import com.itaem.community.modules.complaint.service.ComplaintService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;


/**
 * <p>
 * 投诉 前端控制器
 * </p>
 *
 * @author tomato
 * @since 2020-10-28
 */
@Api(tags = "投诉")
@ResponseResult
@RestController
@RequestMapping("/complaint")
public class ComplaintController {

    @Autowired
    private ComplaintService complaintService;

    /**
     * 分页查询complaint表
     * @param page 第几页
     * @param size 每页多大
     * @return 分页信息
     */
//    @RequiresRoles(value = {"超级管理员","物业工作人员","业主","维修工"}, logical = Logical.OR)
    @ApiOperation("分页查询信息")
    @GetMapping("/{page}/{size}")
    public Page<ComplaintEntity> list(@PathVariable Long page, @PathVariable Long size){
        return complaintService.page(new Page<ComplaintEntity>(page, size));
    }

    /**
     * 根据id获取complaint信息
     * @param id 投诉id
     * @return 单条投诉信息
     */
//    @RequiresRoles(value = {"超级管理员","物业工作人员","业主","维修工"}, logical = Logical.OR)
    @ApiOperation("获取单条信息")
    @GetMapping("/{id}")
    public ComplaintEntity info(@ApiParam(value = "要查询的投诉id") @PathVariable Long id){
        return complaintService.getById(id);
    }

    /**
     * 新增一行complaint数据
     * @param complaintEntity 投诉的所有信息
     * @return 是否投诉成功
     */
//    @RequiresRoles(value = {"超级管理员","物业工作人员","业主","维修工"}, logical = Logical.OR)
    @ApiOperation("新增")
    @PostMapping
    public Boolean save(@RequestBody ComplaintEntity complaintEntity) {
        complaintEntity.setDeleted(false);
        //代码设置发表投诉时间
        complaintEntity.setComplaintTime(LocalDate.now());
        return complaintService.save(complaintEntity);
    }

    /**
     * 根据id更新complaint表数据
     * @param complaintEntity 修改投诉信息
     * @return 是否更新成功
     */
//    @RequiresRoles(value = {"超级管理员","物业工作人员","业主","维修工"}, logical = Logical.OR)
    @ApiOperation("更新")
    @PatchMapping
    public Boolean update(@RequestBody ComplaintEntity complaintEntity) {
        UpdateWrapper<ComplaintEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",complaintEntity.getId());
        return complaintService.update(complaintEntity, updateWrapper);
    }

    /**
     * 根据id逻辑删除complaint表的一行数据
     * @param id 投诉表id
     * @return 是否删除成功
     */
//    @RequiresRoles(value = {"超级管理员","物业工作人员","业主","维修工"}, logical = Logical.OR)
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public Boolean remove(@ApiParam(value = "要删除的投诉id") @PathVariable Long id) {
        return complaintService.removeById(id);
    }
}

